package edu.android;


import java.io.IOException;
import java.io.InputStream;

import texas_holdem.data.Card;
import texas_holdem.data.Card.CardNumber;
import texas_holdem.data.Card.CardType;
import texas_holdem.data.HueristicOddsCalculator;
import texas_holdem.data.OddsCalculator;
import texas_holdem.game.Game;
import texas_holdem.game.Hand;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class StartGameActivity extends Activity implements OnClickListener {
	
	/*
	 * Start - before cards dealt
	 * Flop - before flop
	 * Turn - before turn
	 * River - before river
	 * End - after river
	 */
	public enum GameState { Start, Flop, Turn, River, End };
	private GameState _state;
	private Game _game;
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.start_game);
		Button b = (Button)this.findViewById(R.id.NextStepButton);
		b.setOnClickListener(this);
		_state = GameState.Start;
		TextView view = (TextView)this.findViewById(R.id.OddsTextView);
		view.setVisibility(View.INVISIBLE) ;
	}
	
	public void onClick(View v) {
		TextView view = (TextView)this.findViewById(R.id.OddsTextView);
		if (updateGame(v))
		{
			view.setText("Odds: " + _game.getHand().getOdds());
			view.setVisibility(View.VISIBLE);
		}
		else
		{
			this.finish();
		}
	}
	private boolean updateGame(View v) {
		// TODO Auto-generated method stub
		switch (_state)
		{
			case Start:
				handleStart(v);
				break;
			case Flop:
				handleFlop(v);
				break;
			case Turn:
				handleTurn(v);
				break;
			case River:
				handleRiver(v);
				break;
			case End:
				return false;
		}
		return true;
	}
	
	private void handleStart(View v) {
		getHand();
		Hand hand = _game.getHand();
		
		ImageView hand1 = (ImageView)this.findViewById(R.id.HandCard1);
		hand1.setImageResource(getIdNum(hand.getFirst().getId()));
		ImageView hand2 = (ImageView)this.findViewById(R.id.HandCard2);
		hand2.setImageResource(getIdNum(hand.getSecond().getId()));
		
		Button nextStep = (Button)this.findViewById(R.id.NextStepButton);
		nextStep.setText(R.string.flop);
		_state = GameState.Flop;
	}
	

	
	private void handleFlop(View v) {
		// TODO Auto-generated method stub
		Card flop[] = getFlop();
		_game.flop(flop);
		
		ImageView flop1 = (ImageView)this.findViewById(R.id.FlopCard1);
		flop1.setImageResource(getIdNum(flop[0].getId()));
		ImageView flop2 = (ImageView)this.findViewById(R.id.FlopCard2);
		flop2.setImageResource(getIdNum(flop[1].getId()));
		ImageView flop3 = (ImageView)this.findViewById(R.id.FlopCard3);
		flop3.setImageResource(getIdNum(flop[2].getId()));
		
		Button nextStep = (Button)this.findViewById(R.id.NextStepButton);
		nextStep.setText(R.string.turn);
		_state = GameState.Turn;
	}
	
	private void handleTurn(View v) {
		// TODO Auto-generated method stub
		Card turn = getTurn();
		_game.turn(turn);
		
		ImageView turnCard = (ImageView)this.findViewById(R.id.TurnCard);
		turnCard.setImageResource(getIdNum(turn.getId()));
		
		Button nextStep = (Button)this.findViewById(R.id.NextStepButton);
		nextStep.setText(R.string.river);
		_state = GameState.River;
	}


	private void handleRiver(View v) {
		// TODO Auto-generated method stub
		Card river = getRiver();
		_game.river(river);
		
		ImageView riverCard = (ImageView)this.findViewById(R.id.RiverCard);
		riverCard.setImageResource(getIdNum(river.getId()));
		
		Button nextStep = (Button)this.findViewById(R.id.NextStepButton);
		nextStep.setText(R.string.restart);
		_state = GameState.End;
	}

	// TODO: implement opencv functions
	
	private void getHand() {
		// TODO Auto-generated method stub
		AssetManager assetMgr = getAssets();
		try {
		
			InputStream handEval = assetMgr.open("handEval.txt");
			InputStream initodds = assetMgr.open("initodds.txt");
			HueristicOddsCalculator calculator = new HueristicOddsCalculator(initodds, handEval);
			initodds.close();
			handEval.close();
			Card c1 = new Card(CardNumber.Eight,CardType.Diamond);
			Card c2 = new Card(CardNumber.Eight,CardType.Spade);
			Hand h = new Hand(c1, c2);
			_game = new Game(h, calculator);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private Card[] getFlop() {
		Card flop[] = new Card[3];
		flop[0] = new Card(CardNumber.Eight,CardType.Heart);
		flop[1] = new Card(CardNumber.Jack,CardType.Spade);
		flop[2] = new Card(CardNumber.Ace,CardType.Heart);
		return flop;
		
	}
	private Card getTurn() {
		return new Card(CardNumber.Eight, CardType.Club);
	}
	private Card getRiver() {
		return new Card(CardNumber.Two, CardType.Diamond);
	}
	private int getIdNum(String id) {
		
		if (id.equals("c1"))
			return R.drawable.c1;
		if (id.equals("c2"))
			return R.drawable.c2;
		if (id.equals("c3"))
			return R.drawable.c3;
		if (id.equals("c4"))
			return R.drawable.c4;
		if (id.equals("c5"))
			return R.drawable.c5;
		if (id.equals("c6"))
			return R.drawable.c6;
		if (id.equals("c7"))
			return R.drawable.c7;
		if (id.equals("c8"))
			return R.drawable.c8;
		if (id.equals("c9"))
			return R.drawable.c9;
		if (id.equals("ct"))
			return R.drawable.ct;
		if (id.equals("cj"))
			return R.drawable.cj;
		if (id.equals("cq"))
			return R.drawable.cq;
		if (id.equals("ck"))
			return R.drawable.ck;
		if (id.equals("d1"))
			return R.drawable.d1;
		if (id.equals("d2"))
			return R.drawable.d2;
		if (id.equals("d3"))
			return R.drawable.d3;
		if (id.equals("d4"))
			return R.drawable.d4;
		if (id.equals("d5"))
			return R.drawable.d5;
		if (id.equals("d6"))
			return R.drawable.d6;
		if (id.equals("d7"))
			return R.drawable.d7;
		if (id.equals("d8"))
			return R.drawable.d8;
		if (id.equals("d9"))
			return R.drawable.d9;
		if (id.equals("dt"))
			return R.drawable.dt;
		if (id.equals("dj"))
			return R.drawable.dj;
		if (id.equals("dq"))
			return R.drawable.dq;
		if (id.equals("dk"))
			return R.drawable.dk;
		if (id.equals("h1"))
			return R.drawable.h1;
		if (id.equals("h2"))
			return R.drawable.h2;
		if (id.equals("h3"))
			return R.drawable.h3;
		if (id.equals("h4"))
			return R.drawable.h4;
		if (id.equals("h5"))
			return R.drawable.h5;
		if (id.equals("h6"))
			return R.drawable.h6;
		if (id.equals("h7"))
			return R.drawable.h7;
		if (id.equals("h8"))
			return R.drawable.h8;
		if (id.equals("h9"))
			return R.drawable.h9;
		if (id.equals("ht"))
			return R.drawable.ht;
		if (id.equals("hj"))
			return R.drawable.hj;
		if (id.equals("hq"))
			return R.drawable.hq;
		if (id.equals("hk"))
			return R.drawable.hk;
		if (id.equals("s1"))
			return R.drawable.s1;
		if (id.equals("s2"))
			return R.drawable.s2;
		if (id.equals("s3"))
			return R.drawable.s3;
		if (id.equals("s4"))
			return R.drawable.s4;
		if (id.equals("s5"))
			return R.drawable.s5;
		if (id.equals("s6"))
			return R.drawable.s6;
		if (id.equals("s7"))
			return R.drawable.s7;
		if (id.equals("s8"))
			return R.drawable.s8;
		if (id.equals("s9"))
			return R.drawable.s9;
		if (id.equals("st"))
			return R.drawable.st;
		if (id.equals("sj"))
			return R.drawable.sj;
		if (id.equals("sq"))
			return R.drawable.sq;
		if (id.equals("sk"))
			return R.drawable.sk;
		return -1;
	}

}
